import random
import socket
import json
from time import sleep
x_arr = []
y_arr = []

with open('output.txt', 'r') as f:
    for line in f:
        x_arr.append(float(line.split(',')[0]))
        y_arr.append(float(line.split(',')[1]))
# 构造消息
def create_msg(i):
    data = {
    "hid":'138412817',
    "location":{
        "x":x_arr[i],
        "y":y_arr[i]
    },
    "timestamp":1234567890,
    "tunnel_layer":'101'
    }

    send_msg = json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
    return send_msg

# 创建UDP套接字
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 目标地址和端口
dest_addr = ('127.0.0.1', 10005)
dest_addr_bak = ('127.0.0.1', 11006)

num = 500
# 循环
while(True):
    sleep(0.1)
    num +=1
    send_msg = create_msg(num)
    print(send_msg)
    # 发送数据
    udp_socket.sendto(send_msg.encode('utf-8'), dest_addr)
    udp_socket.sendto(send_msg.encode('utf-8'), dest_addr_bak)

